#include <stdio.h>
#include <winsock.h>

void main(int argc, char **argv)
{
	struct sockaddr_in my_addr;
	struct sockaddr_in peer_addr;
	WSADATA ws_data;
	SOCKET sock;
	char buffer[200];


	int len_recv;
	int addr_len = 0;
	int count = 0;

	if (argc != 2)
	{
		printf("\nArguments: %s <local_UDP_Port>", argv[0]);
		return;
	}

	WSAStartup(MAKEWORD(1, 1), &ws_data);

	sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

	my_addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
	my_addr.sin_port = htons(atoi(argv[1]));
	my_addr.sin_family = AF_INET;

	if (bind(sock, (struct sockaddr*)&my_addr, sizeof(my_addr)) != 0)
	{
		printf("\nBind failed");
		closesocket(sock);
		return;
	}

	
	printf("\nWaiting for UDP mesages...\n");

	count = 0;
	addr_len = sizeof(peer_addr);
	
	while((len_recv = recvfrom(sock, buffer, sizeof(buffer), 0, (struct sockaddr *)&peer_addr, &addr_len)) != -1)
	{
		sendto(sock, buffer, len_recv, 0, (struct sockaddr *)&peer_addr, sizeof(peer_addr));
		count++;
		printf("\nLooped back %d messages", count);
		addr_len = sizeof(peer_addr);
	}
		

	printf("\nHello World\n");

	return;
}


